package top.hcode.hoj;

import cn.hutool.core.date.format.FastDatePrinter;
import cn.hutool.core.text.csv.CsvReader;
import cn.hutool.core.text.csv.CsvUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import lombok.SneakyThrows;
import org.nutz.lang.random.R;
import org.nutz.lang.util.NutMap;
import org.springframework.jdbc.support.incrementer.PostgresSequenceMaxValueIncrementer;
import top.hcode.hoj.common.LazyDao;
import top.hcode.hoj.pojo.entity.org.Org;
import top.hcode.hoj.pojo.entity.org.OrgUser;
import top.hcode.hoj.utils.CommonMethods;

import javax.sql.RowSetListener;
import java.io.FileReader;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author orangej
 * @since 2025/2/21
 */
public class UpdateOrgTreeCodeApp extends BaseDbApp {
    private static LazyDao dao;
    private static String schoolId = "1892805138413035521";
    private static String gradeTreeCode = "2001000100020002";

    public static void main(String[] args) {
        dao = buildLazyDao();

//        insertNewClass("1892805138413035527", 18);
        insetNewStudent("F:\\BaiduNetdiskDownload\\八年级学生名单.csv");

    }

    @SneakyThrows
    private static void insetNewStudent(String file) {
        CsvReader reader = CsvUtil.getReader();
        List<Map<String, String>> rowList = reader.readMapList(new FileReader(file));

        List<Org> classList = dao.selectList(Org.class, "select * from org where " +
                "tree_code like ? and org_type = 7", gradeTreeCode + "%");

        Date now = new Date();

        rowList.parallelStream().forEach(row -> {

            // 序号,姓名,年级,班级
            String num = row.get("序号");
            String name = row.get("姓名");
            name = StrUtil.removeSuffix(name, "."); // 去掉后缀.
            Integer gradeNum = Integer.parseInt(row.get("年级"));
            Integer classNum = Integer.parseInt(row.get("班级"));
            Org clazz = classList.stream()
                    .filter(it -> it.getGradeNum().equals(gradeNum) && it.getClassNum().equals(classNum))
                    .findFirst().orElseThrow(() -> new RuntimeException("找不到班级 : " + gradeNum + "年级" + classNum + "班"));

            System.out.println(name);

            String userId = IdWorker.getIdStr();
            OrgUser user = dao.selectOne(OrgUser.class, "select * from org_user where real_name = ? and flag0 = 1 limit 1", name);
            if (user == null) {
                user = new OrgUser();
                user.setRealName(name);
                String username = RandomUtil.randomString(8);
                while (dao.fetchInt("select count(1) from org_user where username = ?", username) > 0) {
                    username = RandomUtil.randomString(8);
                }
                dao.insert("org_user", NutMap.NEW().addv("id", userId)
                        .addv("username", username)
                        .addv("real_name", name)
                        .addv("user_type", 0)
                        .addv("gmt_create", now)
                        .addv("gmt_modified", now)
                        .addv("flag0", 1)
                        .addv("enabled", 1)
                );

                dao.insert("school_has_user",
                        NutMap.NEW().addv("id", IdWorker.getIdStr())
                                .addv("user_id", userId)
                                .addv("org_id", schoolId)
                                .addv("user_type", 0));

                dao.insert("class_has_user",
                        NutMap.NEW().addv("id", IdWorker.getIdStr())
                                .addv("user_id", userId)
                                .addv("org_id", clazz.getId())
                                .addv("user_type", 0));

            }
        });

    }

    private static void insertNewClass(String gradeId, int num) {
        Org grade = dao.selectOne(Org.class, "select * from org where id = ?", gradeId);
        String gradeName = grade.getOrgName();


        Date now = new Date();
        for (int i = 0; i < num; i++) {
            int classNum = (i + 1);

            Org org = new Org();
            org.setId(IdWorker.getIdStr());
            org.setOrgName(gradeName + classNum + "班");
            org.setOrgType(7);
            org.setParentId(grade.getId());
            org.setTreeCode(grade.getTreeCode() + String.format("%04d", classNum));
            org.setGradeNum(grade.getGradeNum());
            org.setClassNum(classNum);
            org.setGmtCreate(now);
            org.setGmtModified(now);

            dao.insert("org", CommonMethods.obj2HumpMap(org));
        }

    }
}
